import config from "@/config"
import http from "@/utils/request"
import qs from "qs"

export default {
	successCode: config.API_SUCCESS_CODE,
	api: {
		tree: {
			url: `${config.API_URL}/sys/permission/tree`,
			get: async function(){
				const res = await http.get(this.url);
				function transferUI(data,parentData) {
					//兼容 el-cascader 组件
					if (parentData != null){
						data.label = parentData.label + "-" + data.name
					} else {
						data.label = data.name
					}
					data.value = data.pid
					if (data.children && data.children.length > 0) {
						for(var i=0;i<data.children.length;i++){
							transferUI(data.children[i], data)
						}
					}
				}

				if (res.code == config.API_SUCCESS_CODE) {					
					for(var i=0;i<res.data.length;i++){
						transferUI(res.data[i], null)
					}
				}
				return res
			}
		},
		add: {
			url: `${config.API_URL}/sys/permission/add`,
			post: async function(data){
				data = qs.stringify(data)
				return await http.post(this.url,data);
			}
		},
		edit: {
			url: `${config.API_URL}/sys/permission/edit`,
			post: async function(data){
				data = qs.stringify(data)
				return await http.post(this.url,data);
			}
		},
		del: {
			url: `${config.API_URL}/sys/permission/del`,
			post: async function(data){
				data = qs.stringify(data, { indices: false })
				return await http.post(this.url,data);
			}
		},
		list: {
			url: `${config.API_URL}/sys/permission/list`,
			get: async function(data){
				return await http.get(this.url,data);
			}
		}
	}
}
